package com.demo.encryptionDecryption.aes;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author qingqing.liang
 * @create 2017-12-18
 **/
public class AesUtil {

    public static String auoGenerateAESKey() {
        String aesKey = "";

        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            SecretKey secretKey = keyGenerator.generateKey();
            aesKey = asHex(secretKey.getEncoded());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return aesKey;
    }
    private static String asHex(byte[] buf) {
        StringBuffer stringBuffer = new StringBuffer(buf.length * 2);
        for(int i = 0; i < buf.length; ++i) {
            if((buf[i] & 255) < 16) {
                stringBuffer.append("0");
            }
            stringBuffer.append(Long.toString((long)(buf[i] & 255), 16));
        }
        return stringBuffer.toString().toUpperCase();
    }

    public static String encrypt(String data, String aesKey) {
        try {
            KeyGenerator e = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(aesKey.getBytes());
            e.init(128, secureRandom);
            SecretKey secretKey = e.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = data.getBytes("UTF-8");
            cipher.init(1, key);
            byte[] result = cipher.doFinal(byteContent);
            return asHex(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String encryptData, String aesKey) {
        try {
            KeyGenerator e = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(aesKey.getBytes());
            e.init(128, secureRandom);
            SecretKey secretKey = e.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] result = cipher.doFinal(asBin(encryptData));
            return new String(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private static byte[] asBin(String src) {
        byte[] encrypted = new byte[src.length() / 2];

        for (int i = 0; i < src.length() / 2; ++i) {
            int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

}
